package com.medical.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.system.model.SysUser;
import com.medical.system.service.SysUserService;
import com.medical.system.util.MD5Helper;
import com.medical.system.common.Result;
import com.medical.system.vo.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ 类的描述 : 用户管理器
 * @ 作者 : 风花雪月
 * @ 时间 : 2024/7/16 21:40
 * @ 参数 :
 * @return:
 **/
@RestController
@Api(tags = "用户管理器")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    // 分页和条件查询
    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectUserPageByVo(@PathVariable Long page,
                                     @PathVariable Long limit,
                                     SysUserQueryVo sysUserQueryVo) {
        IPage<SysUser> iPage = new Page<>(page, limit);
        iPage = this.sysUserService.selectPage(iPage, sysUserQueryVo);
        return Result.ok(iPage);
    }

    @ApiOperation("根据ID查询用户")
    @GetMapping("findUserById/{id}")
    public Result queryRoleById(@PathVariable Long id) {
        SysUser sysRole = this.sysUserService.getById(id);
        return Result.ok(sysRole);
    }

    @ApiOperation("添加新用户")
    @PostMapping("addUser")
    public Result insertUser(@RequestBody SysUser sysUser) {
//        加密密码
        String encrypt = MD5Helper.encrypt(sysUser.getPassword());
        sysUser.setPassword(encrypt);
        boolean saved = this.sysUserService.save(sysUser);
        if (saved) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/removeUserById/{id}")
    public Result removeUser(@PathVariable Long id) {
        boolean removed = this.sysUserService.removeById(id);
        if (removed) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean isSuccess = this.sysUserService.removeByIds(ids);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("修改角色")
    @PostMapping("updateUser")
    public Result updateRole(@RequestBody SysUser sysUser) {
        boolean isSuccess = this.sysUserService.updateById(sysUser);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status)
    {
        this.sysUserService.updateStatus(id,status);
        return Result.ok();
    }
}
